import 'http_client.dart';

/// API服务类
/// 统一管理所有API接口调用
class ApiService {
  static final HttpClient _httpClient = HttpClient.instance;
  
  // API基础配置
  static const String _baseUrl = 'https://your-api-domain.com/api';
  
  /// 初始化API服务
  static void init({
    String? baseUrl,
    String? token,
    Map<String, dynamic>? headers,
  }) {
    // 设置基础URL
    _httpClient.setBaseUrl(baseUrl ?? _baseUrl);
    
    // 设置Token
    if (token != null) {
      _httpClient.setToken(token);
    }
    
    // 设置通用请求头
    if (headers != null) {
      _httpClient.setHeaders(headers);
    }
    
    // 设置默认请求头
    _httpClient.setHeaders({
      'Platform': 'Flutter',
      'App-Version': '1.0.0',
    });
  }
  
  /// 设置用户Token
  static void setUserToken(String token) {
    _httpClient.setToken(token);
  }
  
  /// 清除用户Token
  static void clearUserToken() {
    _httpClient.clearToken();
  }
  
  // ==================== 用户相关接口 ====================
  
  /// 用户登录
  static Future<HttpResponse> login({
    required String email,
    required String password,
  }) async {
    return await _httpClient.post(
      '/auth/login',
      data: {
        'email': email,
        'password': password,
      },
    );
  }
  
  /// 用户注册
  static Future<HttpResponse> register({
    required String email,
    required String password,
    String? nickname,
  }) async {
    return await _httpClient.post(
      '/auth/register',
      data: {
        'email': email,
        'password': password,
        if (nickname != null) 'nickname': nickname,
      },
    );
  }
  
  /// 获取用户信息
  static Future<HttpResponse> getUserInfo() async {
    return await _httpClient.get('/user/profile');
  }
  
  /// 更新用户信息
  static Future<HttpResponse> updateUserInfo({
    String? nickname,
    String? avatar,
    String? phone,
  }) async {
    return await _httpClient.put(
      '/user/profile',
      data: {
        if (nickname != null) 'nickname': nickname,
        if (avatar != null) 'avatar': avatar,
        if (phone != null) 'phone': phone,
      },
    );
  }
  
  /// 修改密码
  static Future<HttpResponse> changePassword({
    required String oldPassword,
    required String newPassword,
  }) async {
    return await _httpClient.put(
      '/user/password',
      data: {
        'old_password': oldPassword,
        'new_password': newPassword,
      },
    );
  }
  
  /// 注销账号
  static Future<HttpResponse> deleteAccount({
    required String password,
  }) async {
    return await _httpClient.delete(
      '/user/account',
      data: {
        'password': password,
      },
    );
  }
  
  // ==================== 内容相关接口 ====================
  
  /// 获取首页内容
  static Future<HttpResponse> getHomeContent({
    int page = 1,
    int pageSize = 20,
  }) async {
    return await _httpClient.get(
      '/content/home',
      queryParameters: {
        'page': page,
        'page_size': pageSize,
      },
    );
  }
  
  /// 获取分类列表
  static Future<HttpResponse> getCategories() async {
    return await _httpClient.get('/content/categories');
  }
  
  /// 获取分类内容
  static Future<HttpResponse> getCategoryContent({
    required String categoryId,
    int page = 1,
    int pageSize = 20,
  }) async {
    return await _httpClient.get(
      '/content/category/$categoryId',
      queryParameters: {
        'page': page,
        'page_size': pageSize,
      },
    );
  }
  
  /// 搜索内容
  static Future<HttpResponse> searchContent({
    required String keyword,
    int page = 1,
    int pageSize = 20,
    String? category,
  }) async {
    return await _httpClient.get(
      '/content/search',
      queryParameters: {
        'keyword': keyword,
        'page': page,
        'page_size': pageSize,
        if (category != null) 'category': category,
      },
    );
  }
  
  /// 获取内容详情
  static Future<HttpResponse> getContentDetail(String contentId) async {
    return await _httpClient.get('/content/detail/$contentId');
  }
  
  // ==================== 收藏相关接口 ====================
  
  /// 收藏内容
  static Future<HttpResponse> favoriteContent(String contentId) async {
    return await _httpClient.post(
      '/user/favorites',
      data: {
        'content_id': contentId,
      },
    );
  }
  
  /// 取消收藏
  static Future<HttpResponse> unfavoriteContent(String contentId) async {
    return await _httpClient.delete('/user/favorites/$contentId');
  }
  
  /// 获取收藏列表
  static Future<HttpResponse> getFavorites({
    int page = 1,
    int pageSize = 20,
  }) async {
    return await _httpClient.get(
      '/user/favorites',
      queryParameters: {
        'page': page,
        'page_size': pageSize,
      },
    );
  }
  
  // ==================== 文件上传接口 ====================
  
  /// 上传头像
  static Future<HttpResponse> uploadAvatar(String filePath) async {
    return await _httpClient.upload(
      '/upload/avatar',
      filePath,
      fileName: 'avatar.jpg',
    );
  }
  
  /// 上传图片
  static Future<HttpResponse> uploadImage(
    String filePath, {
    String? fileName,
  }) async {
    return await _httpClient.upload(
      '/upload/image',
      filePath,
      fileName: fileName,
    );
  }
  
  // ==================== 系统相关接口 ====================
  
  /// 获取应用配置
  static Future<HttpResponse> getAppConfig() async {
    return await _httpClient.get('/system/config');
  }
  
  /// 检查版本更新
  static Future<HttpResponse> checkUpdate() async {
    return await _httpClient.get('/system/version');
  }
  
  /// 反馈建议
  static Future<HttpResponse> submitFeedback({
    required String content,
    String? contact,
    List<String>? images,
  }) async {
    return await _httpClient.post(
      '/system/feedback',
      data: {
        'content': content,
        if (contact != null) 'contact': contact,
        if (images != null) 'images': images,
      },
    );
  }
  
  // ==================== 通用请求方法 ====================
  
  /// 通用GET请求
  static Future<HttpResponse> get(
    String path, {
    Map<String, dynamic>? queryParameters,
    Map<String, dynamic>? headers,
  }) async {
    return await _httpClient.get(
      path,
      queryParameters: queryParameters,
      headers: headers,
    );
  }
  
  /// 通用POST请求
  static Future<HttpResponse> post(
    String path, {
    dynamic data,
    Map<String, dynamic>? queryParameters,
    Map<String, dynamic>? headers,
  }) async {
    return await _httpClient.post(
      path,
      data: data,
      queryParameters: queryParameters,
      headers: headers,
    );
  }
  
  /// 通用PUT请求
  static Future<HttpResponse> put(
    String path, {
    dynamic data,
    Map<String, dynamic>? queryParameters,
    Map<String, dynamic>? headers,
  }) async {
    return await _httpClient.put(
      path,
      data: data,
      queryParameters: queryParameters,
      headers: headers,
    );
  }
  
  /// 通用DELETE请求
  static Future<HttpResponse> delete(
    String path, {
    dynamic data,
    Map<String, dynamic>? queryParameters,
    Map<String, dynamic>? headers,
  }) async {
    return await _httpClient.delete(
      path,
      data: data,
      queryParameters: queryParameters,
      headers: headers,
    );
  }

  // ==================== 简化版请求方法 ====================
  
  /// 通用请求方法 - 简化版
  /// [path] 接口路径（不包含host地址）
  /// [method] 请求方法 GET/POST/PUT/DELETE
  /// [params] 请求参数Map
  /// [headers] 自定义请求头
  static Future<HttpResponse> request(
    String path, {
    String method = 'GET',
    Map<String, dynamic>? params,
    Map<String, dynamic>? headers,
  }) async {
    return HttpClient.instance.request(
      path,
      method: method,
      params: params,
      headers: headers,
    );
  }

  /// GET请求 - 简化版
  /// [path] 接口路径（不包含host地址）
  /// [params] 查询参数Map
  /// [headers] 自定义请求头
  static Future<HttpResponse> simpleGet(
    String path, {
    Map<String, dynamic>? params,
    Map<String, dynamic>? headers,
  }) async {
    return HttpClient.instance.simpleGet(path, params: params, headers: headers);
  }

  /// POST请求 - 简化版
  /// [path] 接口路径（不包含host地址）
  /// [params] 请求参数Map
  /// [headers] 自定义请求头
  static Future<HttpResponse> simplePost(
    String path, {
    Map<String, dynamic>? params,
    Map<String, dynamic>? headers,
  }) async {
    return HttpClient.instance.simplePost(path, params: params, headers: headers);
  }

  /// PUT请求 - 简化版
  /// [path] 接口路径（不包含host地址）
  /// [params] 请求参数Map
  /// [headers] 自定义请求头
  static Future<HttpResponse> simplePut(
    String path, {
    Map<String, dynamic>? params,
    Map<String, dynamic>? headers,
  }) async {
    return HttpClient.instance.simplePut(path, params: params, headers: headers);
  }

  /// DELETE请求 - 简化版
  /// [path] 接口路径（不包含host地址）
  /// [params] 请求参数Map
  /// [headers] 自定义请求头
  static Future<HttpResponse> simpleDelete(
    String path, {
    Map<String, dynamic>? params,
    Map<String, dynamic>? headers,
  }) async {
    return HttpClient.instance.simpleDelete(path, params: params, headers: headers);
  }

  // ==================== 回调版本的请求方法 ====================

  /// 通用请求方法 - 回调版
  /// [path] 接口路径（不包含host地址）
  /// [method] 请求方法 GET/POST/PUT/DELETE
  /// [params] 请求参数Map
  /// [headers] 自定义请求头
  /// [onSuccess] 成功回调
  /// [onError] 失败回调
  static void requestWithCallback(
    String path, {
    String method = 'GET',
    Map<String, dynamic>? params,
    Map<String, dynamic>? headers,
    Function(HttpResponse response)? onSuccess,
    Function(HttpResponse error)? onError,
  }) {
    HttpClient.instance.requestWithCallback(
      path,
      method: method,
      params: params,
      headers: headers,
      onSuccess: onSuccess,
      onError: onError,
    );
  }

  /// GET请求 - 回调版
  /// [path] 接口路径（不包含host地址）
  /// [params] 查询参数Map
  /// [headers] 自定义请求头
  /// [onSuccess] 成功回调
  /// [onError] 失败回调
  static void simpleGetWithCallback(
    String path, {
    Map<String, dynamic>? params,
    Map<String, dynamic>? headers,
    Function(HttpResponse response)? onSuccess,
    Function(HttpResponse error)? onError,
  }) {
    HttpClient.instance.simpleGetWithCallback(
      path,
      params: params,
      headers: headers,
      onSuccess: onSuccess,
      onError: onError,
    );
  }

  /// POST请求 - 回调版
  /// [path] 接口路径（不包含host地址）
  /// [params] 请求参数Map
  /// [headers] 自定义请求头
  /// [onSuccess] 成功回调
  /// [onError] 失败回调
  static void simplePostWithCallback(
    String path, {
    Map<String, dynamic>? params,
    Map<String, dynamic>? headers,
    Function(HttpResponse response)? onSuccess,
    Function(HttpResponse error)? onError,
  }) {
    HttpClient.instance.simplePostWithCallback(
      path,
      params: params,
      headers: headers,
      onSuccess: onSuccess,
      onError: onError,
    );
  }

  /// PUT请求 - 回调版
  /// [path] 接口路径（不包含host地址）
  /// [params] 请求参数Map
  /// [headers] 自定义请求头
  /// [onSuccess] 成功回调
  /// [onError] 失败回调
  static void simplePutWithCallback(
    String path, {
    Map<String, dynamic>? params,
    Map<String, dynamic>? headers,
    Function(HttpResponse response)? onSuccess,
    Function(HttpResponse error)? onError,
  }) {
    HttpClient.instance.simplePutWithCallback(
      path,
      params: params,
      headers: headers,
      onSuccess: onSuccess,
      onError: onError,
    );
  }

  /// DELETE请求 - 回调版
  /// [path] 接口路径（不包含host地址）
  /// [params] 请求参数Map
  /// [headers] 自定义请求头
  /// [onSuccess] 成功回调
  /// [onError] 失败回调
  static void simpleDeleteWithCallback(
    String path, {
    Map<String, dynamic>? params,
    Map<String, dynamic>? headers,
    Function(HttpResponse response)? onSuccess,
    Function(HttpResponse error)? onError,
  }) {
    HttpClient.instance.simpleDeleteWithCallback(
      path,
      params: params,
      headers: headers,
      onSuccess: onSuccess,
      onError: onError,
    );
  }
}